package com.xielun.fly.framework.sql;


import com.xielun.fly.framework.Constants;

import java.util.Set;

public class SQLBuilder {


    /**
     * 生成select 语句
     *
     * @return
     */
    public final static String selectSQL(SQL meta) {
        final StringBuilder sqlBuilder = new StringBuilder("SELECT ");
        sqlBuilder.append(meta.getColumnsStr());
        sqlBuilder.append(" FROM " + meta.getTable());
        sqlBuilder.append(meta.getWhere());
        sqlBuilder.append(meta.getOrderStr());
        sqlBuilder.append(meta.getPageStr());
        return sqlBuilder.toString();
    }

    /**
     * 生成update 语句
     *
     * @return
     */
    public final static String updateSQL(SQL meta) {

        final StringBuilder sqlBuilder = new StringBuilder("UPDATE " + meta.getTable());
        sqlBuilder.append(meta.getDataStr());
        sqlBuilder.append(meta.getWhere());
        return sqlBuilder.toString();
    }

    /**
     * 生成delete语句
     *
     * @return
     */
    public static String deleteSQL(SQL meta) {
        final StringBuilder sqlBuilder = new StringBuilder("DELETE  FROM " + meta.getTable());
        sqlBuilder.append(meta.getWhere());
        return sqlBuilder.toString();
    }

    /**
     * 生成insert 语句
     *
     * @return
     */
    public final static String insertSQL(SQL meta) {
        final StringBuilder sqlBuilder = new StringBuilder(" INSERT INTO ");
        sqlBuilder.append(meta.getTable());
        sqlBuilder.append(meta.getInsertDataStr());
        return sqlBuilder.toString();

    }


    public final static String genSQL(SQL meta) {

        if (meta.getOperate().equals(Constants.ADD)) {
            return insertSQL(meta);

        } else if (meta.getOperate().equals(Constants.DEL)) {
            return deleteSQL(meta);
        } else if (meta.getOperate().equals(Constants.MOD)) {
            return updateSQL(meta);
        } else {
            return selectSQL(meta);
        }
    }

}


